package com.example.statemachine.event;

import com.example.statemachine.service.StateTransitionLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class StateChangeEventListener {

    @Autowired
    private StateTransitionLogService logService;

    @EventListener
    @Async
    public void handleStateChange(StateChangeEvent event) {
        try {
            // 记录状态转换日志
            logService.recordTransitionLog(event);

            log.info("状态转换完成: {} -> {}, 操作人: {}",
                    event.getFromState(), event.getToState(),
                    event.getContext().getOperator());

        } catch (Exception e) {
            log.error("处理状态变更事件失败", e);
        }
    }
}